//
//  CustomPickerView.m
//  DefendHealth
//
//  Created by 吾诺瀚卓 on 17/2/25.
//  Copyright © 2017年 iMacOne. All rights reserved.
//

#import "CustomPickerView.h"
#import "Header.h"

@interface CustomPickerView()<UIScrollViewDelegate>

@end

@implementation CustomPickerView
{
    NSArray *_dataArray;
    NSInteger _components;
    CGFloat _height;
    CGFloat _width;
    CGFloat _componentWidth;
    CGFloat _rowHeight;
    NSInteger _flagRow;
    NSInteger _lastFlagRow;
    CGFloat _ScrollRowHeight;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _ScrolleViewArray = [[NSMutableArray alloc]init];
        _flagRow = 0;
        _lastFlagRow = 0;
        self.userInteractionEnabled =YES;
        self.clipsToBounds =YES;
        _height = 140+40;
        _ScrollRowHeight =(_height-40)/5.00;
        _width = frame.size.width-60;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

-(void)setDataSource:(id<UIMyPickerDataSource>)dataSource{
//    self.backgroundColor = kRedColor;
    _dataArray =@[@"月",@"日",@"分"];
    _dataSource = dataSource;
    _components = [_dataSource numberOfComponentsInMyPickerView:self];
    _componentWidth = ((_width-_components-1)*100)/(_components*100.00);
    
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    topView.backgroundColor = kWhiteColor;
    [self addSubview:topView];
    UILabel * cancelLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 28, 35, 11)];
    cancelLab.font = FONT(11);
    cancelLab.text = @"取消";
    [topView addSubview:cancelLab];
    UIButton * cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:cancelBtn];
    
    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(kMaxX(cancelLab.frame)+10, 25, kScreenWidth-70-20-20, 14)];
    title.text = @"开始时间";
    title.font = FONT(14);
    title.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:title];
    
    UILabel * savelLab = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-10-35, 28, 35, 11)];
    savelLab.font = FONT(11);
    savelLab.text = @"确定";
    [topView addSubview:savelLab];
    UIButton * saveBtn = [[UIButton alloc]initWithFrame:CGRectMake(kScreenWidth-40, 0, 40, 40)];
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];

    [topView addSubview:saveBtn];
    
    UIView *leftView= [[UIView alloc]initWithFrame:CGRectMake(0,55+40, 40, 40)];
//    leftView.backgroundColor = [UIColor colorWithRed:191/255.0 green:215/255.0 blue:242/255.0 alpha:1];
    leftView.backgroundColor = [UIColor clearColor];
    leftView.layer.cornerRadius =10;
    [self addSubview:leftView];
    for (NSInteger i =0; i<_components+1; i++) {
//        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),10, 1, _height)];
//        label.backgroundColor = [UIColor colorWithRed:127/255.0 green:127/255.0 blue:127/255.0 alpha:0.8];
//        [self addSubview:label];
        
        if (i!=_components) {
            NSInteger _rows = [_dataSource myPickerView:self numberOfRowInComponent:i];
            //NSLog(@"_rows--%ld",_rows);
            
            UIScrollView *bottomScrollView1 = [[UIScrollView alloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),5+40, _componentWidth,20)];
            //bottomScrollView1.delegate = self;
            bottomScrollView1.tag = 501+i*10;
//            bottomScrollView1.decelerationRate =0.1;
            bottomScrollView1.showsHorizontalScrollIndicator =NO;
            bottomScrollView1.showsVerticalScrollIndicator =NO;
            bottomScrollView1.bounces = NO;
            bottomScrollView1.alwaysBounceHorizontal =NO;
            bottomScrollView1.alwaysBounceHorizontal =NO;
            bottomScrollView1.userInteractionEnabled =NO;
            
            bottomScrollView1.backgroundColor = [UIColor whiteColor];
            
            bottomScrollView1.contentSize = CGSizeMake(_componentWidth, 20*_rows);
            bottomScrollView1.contentOffset = CGPointMake(0, -40);
            
//            [self addSubview:bottomScrollView1];
            
            for (NSInteger j =0; j<_rows; j++) {
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];
                [bottomScrollView1 addSubview:label];
                label.tag = 600+(100*i)+j;
                label.alpha = 0.3;
                label.font = [UIFont systemFontOfSize:16];
//                label.textColor = [UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1];
                label.textColor = [UIColor blackColor];
                label.textAlignment = NSTextAlignmentCenter;
                label.text = [_dataSource myPickerView:self titleForRow:j forComponent:i];
                label.backgroundColor = [UIColor whiteColor];
            }
            
            UIScrollView *middleScrollView1 =[[UIScrollView alloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),CGRectGetMaxY(bottomScrollView1.frame),_componentWidth-35, 30)];
            //middleScrollView1.delegate = self;
            middleScrollView1.tag = 502+i*10;
//            middleScrollView1.decelerationRate =0.1;
            middleScrollView1.showsHorizontalScrollIndicator =NO;
            middleScrollView1.showsVerticalScrollIndicator =NO;
            middleScrollView1.bounces = NO;
            middleScrollView1.alwaysBounceHorizontal =NO;
            middleScrollView1.alwaysBounceHorizontal =NO;
            middleScrollView1.userInteractionEnabled =NO;
            
            middleScrollView1.backgroundColor = [UIColor whiteColor];
            
            middleScrollView1.contentSize = CGSizeMake(_componentWidth, 30*(_rows));
            middleScrollView1.contentOffset = CGPointMake(0, -30);
            
            [self addSubview:middleScrollView1];
            for (NSInteger j =0; j<_rows; j++) {
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,30*j, _componentWidth-35,30)];
                [middleScrollView1 addSubview:label];
                label.tag = 600+(100*i)+j+20;
                label.alpha = 0.5;
//                label.textColor = [UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1];
                label.textColor = [UIColor blackColor];

                label.font = [UIFont systemFontOfSize:25];
                label.textAlignment = NSTextAlignmentRight;
                label.text = [_dataSource myPickerView:self titleForRow:j forComponent:i];
                label.backgroundColor = [UIColor whiteColor];
            }
            
            
            
            UIScrollView *topScrollView =[[ UIScrollView alloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),55+40, _componentWidth-30,40)];
            //topScrollView.delegate = self;
            topScrollView.tag = 503+i*10;
//            topScrollView.backgroundColor = [UIColor colorWithRed:191/255.0 green:215/255.0 blue:242/255.0 alpha:1];
            
            topScrollView.backgroundColor = [UIColor whiteColor];
//            topScrollView.decelerationRate = 0.1;
            topScrollView.showsHorizontalScrollIndicator =NO;
            topScrollView.showsVerticalScrollIndicator =NO;
            topScrollView.bounces = NO;
            topScrollView.userInteractionEnabled =NO;
            topScrollView.alwaysBounceHorizontal =NO;
            topScrollView.alwaysBounceHorizontal =NO;
            topScrollView.contentOffset = CGPointMake(0, 0);
            topScrollView.contentSize = CGSizeMake(_componentWidth, 40*_rows);
            [self addSubview:topScrollView];
            for (NSInteger j =0; j<_rows; j++) {
                //NSLog(@"j==%ld",j);
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,40*j, _componentWidth-30,40)];
                [topScrollView addSubview:label];
                label.tag = 600+(100*i)+j+40;
//                label.textColor = [UIColor colorWithRed:36/255.0 green:119/255.0 blue:212/255.0 alpha:1];
                label.textColor = [UIColor blackColor];

                label.font = [UIFont systemFontOfSize:28];
//                label.backgroundColor = [UIColor colorWithRed:191/255.0 green:215/255.0 blue:242/255.0 alpha:0];
                label.backgroundColor = [UIColor clearColor];
                label.textAlignment = NSTextAlignmentRight;
                label.text = [_dataSource myPickerView:self titleForRow:j forComponent:i];//[NSString stringWithFormat:@"%ld",label.tag];//
                label.transform = CGAffineTransformScale(label.transform, 0.9, 1.5);
                
                label.font = [UIFont fontWithName:@"ArialRoundedMTBold" size:28];

            }
            
            UILabel *unitLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(topScrollView.frame),CGRectGetMaxY(topScrollView.frame)-20,20, 20)];
            [self addSubview:unitLabel];
            unitLabel.text = _dataArray[i];
            //unitLabel.backgroundColor = [UIColor whiteColor];
//            unitLabel.textColor = [UIColor colorWithRed:36/255.0 green:119/255.0 blue:212/255.0 alpha:1];
            unitLabel.textColor = [UIColor blackColor];
            unitLabel.font = [UIFont boldSystemFontOfSize:16];
            
            UIScrollView *middleScrollView2 =[[UIScrollView alloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),CGRectGetMaxY(topScrollView.frame),_componentWidth-35, 30)];
            //middleScrollView2.delegate = self;
            middleScrollView2.tag = 504+i*10;
//            middleScrollView2.decelerationRate =0.1;
            middleScrollView2.showsHorizontalScrollIndicator =NO;
            middleScrollView2.showsVerticalScrollIndicator =NO;
            middleScrollView2.bounces = NO;
            middleScrollView2.alwaysBounceHorizontal =NO;
            middleScrollView2.alwaysBounceHorizontal =NO;
            middleScrollView2.userInteractionEnabled =NO;
            
            middleScrollView2.backgroundColor = [UIColor whiteColor];
            
            middleScrollView2.contentOffset = CGPointMake(0, 30);
            [self addSubview:middleScrollView2];
            middleScrollView2.contentSize = CGSizeMake(_componentWidth, 30*(_rows));
            for (NSInteger j =0; j<_rows; j++) {
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,30*j, _componentWidth-35,30)];
                [middleScrollView2 addSubview:label];
                label.tag = 600+(100*i)+j+60;
                label.alpha = 0.5;
                label.font = [UIFont systemFontOfSize:25];
//                label.textColor = [UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1];
                label.textColor = [UIColor blackColor];
                if (middleScrollView2.tag == 516) {
                    label.textAlignment = NSTextAlignmentLeft;

                }else{
                    label.textAlignment = NSTextAlignmentRight;

                }
                label.text = [_dataSource myPickerView:self titleForRow:j forComponent:i];
                label.backgroundColor = [UIColor whiteColor];
            }
            
            
            //27376
            
            UIScrollView *bottomScrollView2 = [[UIScrollView alloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),CGRectGetMaxY(middleScrollView2.frame),_componentWidth, 20)];
            //bottomScrollView2.delegate = self;
            bottomScrollView2.tag = 505+i*10;
//            bottomScrollView2.decelerationRate =0.1;
            bottomScrollView2.showsHorizontalScrollIndicator =NO;
            bottomScrollView2.showsVerticalScrollIndicator =NO;
            bottomScrollView2.bounces = NO;
            bottomScrollView2.alwaysBounceHorizontal =NO;
            bottomScrollView2.alwaysBounceHorizontal =NO;
            bottomScrollView2.userInteractionEnabled =NO;
            
            bottomScrollView2.backgroundColor = [UIColor whiteColor];
            
            bottomScrollView2.contentOffset = CGPointMake(0, 40);
            bottomScrollView2.contentSize = CGSizeMake(_componentWidth, 20*_rows);
//            [self addSubview:bottomScrollView2];
            for (NSInteger j =0; j<_rows; j++) {
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,20*j, _componentWidth,20)];
                [bottomScrollView2 addSubview:label];
                label.tag = 600+(100*i)+j+80;
                label.alpha = 0.3;
//                label.textColor = [UIColor colorWithRed:150/255.0 green:150/255.0 blue:150/255.0 alpha:1];
                label.textColor = [UIColor blackColor];
                label.textAlignment = NSTextAlignmentCenter;
                label.font = [UIFont systemFontOfSize:16];
                label.text = [_dataSource myPickerView:self titleForRow:j forComponent:i];
                label.backgroundColor = [UIColor whiteColor];
            }
            
            UIScrollView *_scrollView= [[UIScrollView alloc]initWithFrame:CGRectMake(30+i*(_componentWidth+1),0, _componentWidth,_height)];
            _scrollView.userInteractionEnabled =YES;
            [self addSubview:_scrollView];
            _scrollView.tag = 506+i*10;
            
            _scrollView.decelerationRate = 0.01;
            //_scrollView.showsHorizontalScrollIndicator = NO;
            _scrollView.showsVerticalScrollIndicator =NO;
            _scrollView.bounces = NO;
            _scrollView.contentSize = CGSizeMake(_componentWidth, 40*(_rows+2.5));
            
            _scrollView.contentOffset = CGPointMake(0, 0);
            _scrollView.delegate = self;
            
            
            NSArray *array = @[bottomScrollView1,middleScrollView1,topScrollView,middleScrollView2,bottomScrollView2];
            [_ScrolleViewArray addObject:array];
            
        }
        if(i==_components-1){
            UIView *rightView= [[UIView alloc]initWithFrame:CGRectMake(_width+20,55, 40, 40)];
//            rightView.backgroundColor = [UIColor colorWithRed:191/255.0 green:215/255.0 blue:242/255.0 alpha:1];
            rightView.backgroundColor =[UIColor clearColor];
            rightView.layer.cornerRadius =10;
            [self addSubview:rightView];
        }
        
    }
}
- (void)setDelegate:(id<UIMyPickerDelegate>)delegate{
    _delegate = delegate;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSet = scrollView.contentOffset.y;
    CGFloat _offset = offSet/40.00;
    _flagRow = (NSInteger)(offSet/40.00+0.5);
    if (_flagRow!=_lastFlagRow) {
        _lastFlagRow =_flagRow;
        // NSLog(@"_flagRow%ld",_flagRow);
    }
    //NSLog(@"tag_%ld",scrollView.tag);
    
    NSInteger flag = scrollView.tag-500;
    NSInteger componentFlag = flag/10;
    NSArray *array = (NSArray *)_ScrolleViewArray[componentFlag];
    for (NSInteger i=0; i<5; i++) {
        UIScrollView *scrollView = (UIScrollView *)array[i];
        
        if (i==0){
            scrollView.contentOffset = CGPointMake(0, 20*_offset-40);
        }
        if(i==1) {
            scrollView.contentOffset = CGPointMake(0, 30*_offset-30);
        }
        if (i==2){
            scrollView.contentOffset = CGPointMake(0, offSet);
        }
        if(i==3) {
            scrollView.contentOffset = CGPointMake(0, 30*_offset+30);
        }
        if(i==4){
            scrollView.contentOffset = CGPointMake(0, 20*_offset+40);
        }
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"停止拖拽");
    [self setUpDelegateWith:scrollView];
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"开始减速");
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"减速结束");
    [self setUpDelegateWith:scrollView];
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        if (_delegate && [_delegate respondsToSelector:@selector(myPickerView:titleForDidSelectRow:forComponent:)]) {
    //            [_delegate myPickerView:self titleForDidSelectRow:_offSetY forComponent:componentFlag];
    //        }
    //    });
}

-(void)setUpDelegateWith:(UIScrollView *)scrollView{
    CGFloat offSetY = scrollView.contentOffset.y;
    NSInteger _offSetY = offSetY/40.00+0.5;
    NSInteger flag = scrollView.tag-500;
    NSInteger componentFlag = flag/10;
    NSArray *array = (NSArray *)_ScrolleViewArray[componentFlag];
    for (NSInteger i=0; i<5; i++) {
        UIScrollView *scrollView = (UIScrollView *)array[i];
        
        if (i==0){//NSLog(@"%ld",scrollView.tag);
            [scrollView setContentOffset:CGPointMake(0,20*_offSetY-40)animated:YES];
        }
        if(i==1) {//NSLog(@"%ld",scrollView.tag);
            [scrollView setContentOffset:CGPointMake(0,30*_offSetY-30)animated:YES];
        }
        if (i==2){//NSLog(@"%ld",scrollView.tag);
            [scrollView setContentOffset:CGPointMake(0,40*_offSetY) animated:YES];
        }
        if(i==3) {//NSLog(@"%ld",scrollView.tag);
            [scrollView setContentOffset:CGPointMake(0,30*_offSetY+30)animated:YES];
        }
        if(i==4){//NSLog(@"%ld",scrollView.tag);
            [scrollView setContentOffset:CGPointMake(0,20*_offSetY+40)animated:YES];
        }
    }
    if (_delegate && [_delegate respondsToSelector:@selector(myPickerView:titleForDidSelectRow:forComponent:)]) {
        [_delegate myPickerView:self titleForDidSelectRow:_offSetY forComponent:componentFlag];}
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    NSLog(@"滑动动画结束");
}

-(void)cancel{
    [self removeFromSuperview];
}
-(void)save{
    [self removeFromSuperview];
}
@end
